﻿namespace Dcms.UpdateModule
{
    using System;
    using System.Data;
    using System.Text.RegularExpressions;

    public class DcmsAllModelSqlManage
    {
        public static string CommSpecialFieldResolve(string ModelName, string HtmlTempLate, DataRow Datadr)
        {
            switch (ModelName.ToLower())
            {
                case "intro":
                {
                    DcmsIntroProcessCode code = new DcmsIntroProcessCode();
                    return code.SpecialFieldResolve(HtmlTempLate, ModelName, Datadr);
                }
                case "cate":
                {
                    DcmsCateProcessCode code2 = new DcmsCateProcessCode();
                    return code2.SpecialFieldResolve(HtmlTempLate, ModelName, Datadr);
                }
                case "news":
                {
                    DcmsNewsProcessCode code3 = new DcmsNewsProcessCode();
                    return code3.SpecialFieldResolve(HtmlTempLate, ModelName, Datadr);
                }
                case "products":
                {
                    DcmsProductProcessCode code4 = new DcmsProductProcessCode();
                    return code4.SpecialFieldResolve(HtmlTempLate, ModelName, Datadr);
                }
                case "link":
                {
                    DcmsLinkProcessCode code5 = new DcmsLinkProcessCode();
                    return code5.SpecialFieldResolve(HtmlTempLate, ModelName, Datadr);
                }
                case "down":
                {
                    DcmsDownProcessCode code6 = new DcmsDownProcessCode();
                    return code6.SpecialFieldResolve(HtmlTempLate, ModelName, Datadr);
                }
                case "vote":
                {
                    DcmsVoteProcessCode code7 = new DcmsVoteProcessCode();
                    return code7.SpecialFieldResolve(HtmlTempLate, ModelName, Datadr);
                }
                case "guestbook":
                {
                    DcmsGuestBookProcessCode code8 = new DcmsGuestBookProcessCode();
                    return code8.SpecialFieldResolve(HtmlTempLate, ModelName, Datadr);
                }
                case "position":
                {
                    DcmsPositionProcessCode code9 = new DcmsPositionProcessCode();
                    return code9.SpecialFieldResolve(HtmlTempLate, ModelName, Datadr);
                }
            }
            return "";
        }

        public static string CreateSqlStr(string ConditionStr, string ItemStyleHtml)
        {
            string conditionStr = "";
            string isPage = "";
            string numStr = "1";
            string orderFileStr = "Id";
            string sortStr = "Desc";
            string modelName = "";
            string fileList = "";
            string paraName = "";
            string defaultValue = "";
            string paraTypeStr = "";
            string selectDir = "";
            modelName = GetModelName(ConditionStr, "Module=\"(?<ModelName>(\\w*))\"", "ModelName");
            conditionStr = GetModelName(ConditionStr, "Condition=\"(?<Condition>(.*?\"))", "Condition").Replace('.', '_');
            isPage = GetModelName(ConditionStr, "IsPage=\"(?<IsPage>(\\w*))\"", "IsPage");
            numStr = GetModelName(ConditionStr, "Num=\"(?<Num>(\\d*))\"", "Num");
            orderFileStr = GetModelName(ConditionStr, "Order=\"(?<Order>(\\w*.\\w*))\"", "Order").Replace('.', '_');
            sortStr = GetModelName(ConditionStr, "Sort=\"(?<Sort>(\\w*))\"", "Sort");
            paraName = GetModelName(ConditionStr, "ParaName=\"(?<ParaName>((\\w*),?)*)\"", "ParaName");
            defaultValue = GetModelName(ConditionStr, "DefaultValue=\"(?<DefaultValue>((\\w*),?\\|?)*)\"", "DefaultValue");
            selectDir = GetModelName(ConditionStr, "SelectDir=\"(?<SelectDir>(\\w*))\"", "SelectDir");
            paraTypeStr = GetModelName(ConditionStr, "ParaType=\"(?<ParaType>((\\w*),?)*)\"", "ParaType");
            fileList = FileNameList(ItemStyleHtml, modelName);
            switch (modelName.ToLower())
            {
                case "intro":
                {
                    DcmsIntroProcessCode code = new DcmsIntroProcessCode();
                    return code.IntroModuleSql(modelName, fileList, paraName, defaultValue, selectDir, conditionStr, numStr, isPage, orderFileStr, sortStr, paraTypeStr);
                }
                case "cate":
                {
                    DcmsCateProcessCode code2 = new DcmsCateProcessCode();
                    return code2.CateModuleSql(modelName, fileList, paraName, defaultValue, selectDir, conditionStr, numStr, isPage, orderFileStr, sortStr, paraTypeStr);
                }
                case "news":
                {
                    DcmsNewsProcessCode code3 = new DcmsNewsProcessCode();
                    return code3.NewsModuleSql(modelName, fileList, paraName, defaultValue, selectDir, conditionStr, numStr, isPage, orderFileStr, sortStr, paraTypeStr);
                }
                case "products":
                {
                    DcmsProductProcessCode code4 = new DcmsProductProcessCode();
                    return code4.ProductModuleSql(modelName, fileList, paraName, defaultValue, selectDir, conditionStr, numStr, isPage, orderFileStr, sortStr, paraTypeStr);
                }
                case "link":
                {
                    DcmsLinkProcessCode code5 = new DcmsLinkProcessCode();
                    return code5.LinkModuleSql(modelName, fileList, paraName, defaultValue, selectDir, conditionStr, numStr, isPage, orderFileStr, sortStr, paraTypeStr);
                }
                case "down":
                {
                    DcmsDownProcessCode code6 = new DcmsDownProcessCode();
                    return code6.DownModuleSql(modelName, fileList, paraName, defaultValue, selectDir, conditionStr, numStr, isPage, orderFileStr, sortStr, paraTypeStr);
                }
                case "vote":
                {
                    DcmsVoteProcessCode code7 = new DcmsVoteProcessCode();
                    return code7.VoteModuleSql(modelName, fileList, paraName, defaultValue, selectDir, conditionStr, numStr, isPage, orderFileStr, sortStr, paraTypeStr);
                }
                case "guestbook":
                {
                    DcmsGuestBookProcessCode code8 = new DcmsGuestBookProcessCode();
                    return code8.GuestBookModuleSql(modelName, fileList, paraName, defaultValue, selectDir, conditionStr, numStr, isPage, orderFileStr, sortStr, paraTypeStr);
                }
                case "position":
                {
                    DcmsPositionProcessCode code9 = new DcmsPositionProcessCode();
                    return code9.PositionModuleSql(modelName, fileList, paraName, defaultValue, selectDir, conditionStr, numStr, isPage, orderFileStr, sortStr, paraTypeStr);
                }
                case "siteinfo":
                {
                    DcmsSiteInfoProcessCode code10 = new DcmsSiteInfoProcessCode();
                    return code10.SiteInfoModuleSql(modelName, fileList, paraName, defaultValue, selectDir, conditionStr, numStr, isPage, orderFileStr, sortStr, paraTypeStr);
                }
            }
            return "";
        }

        protected static string FileNameList(string HtmlTemp, string ModelName)
        {
            string regexStr = "";
            regexStr = "{" + ModelName + @".(?<FileName>(\w+))(\s*)(,?)(.*?)}";
            return GetBindFileList(HtmlTemp, regexStr, ModelName);
        }

        private static string GetBindFileList(string ItemHtmlTempLate, string RegexStr, string ModelName)
        {
            string oldFileList = "";
            MatchCollection matchs = new Regex(RegexStr, RegexOptions.ExplicitCapture).Matches(ItemHtmlTempLate);
            string str2 = "";
            if (matchs.Count > 0)
            {
                for (int i = 0; i < matchs.Count; i++)
                {
                    str2 = ModelName + "_" + matchs[i].Groups["FileName"].Value;
                    if (oldFileList.IndexOf(str2, StringComparison.OrdinalIgnoreCase) == -1) oldFileList = oldFileList + ModelName + "_" + matchs[i].Groups["FileName"].Value + ",";
                }
                if (oldFileList.Length > 1) oldFileList = oldFileList.Substring(0, oldFileList.Length - 1);
                return GetBindSpeFileList(ItemHtmlTempLate, ModelName, oldFileList);
            }
            return "0";
        }

        private static string GetBindSpeFileList(string ItemHtmlTempLate, string ModelName, string OldFileList)
        {
            MatchCollection matchs = new Regex("{" + ModelName + @".(?<FileName>(\w+))\.(\w+?)}", RegexOptions.ExplicitCapture).Matches(ItemHtmlTempLate);
            if (matchs.Count > 0)
            {
                OldFileList = OldFileList + ",";
                for (int i = 0; i < matchs.Count; i++)
                {
                    if (OldFileList.IndexOf(ModelName + "_" + matchs[i].Groups["FileName"].Value, StringComparison.OrdinalIgnoreCase) == -1) OldFileList = OldFileList + ModelName + "_" + matchs[i].Groups["FileName"].Value + ",";
                }
                if (OldFileList.LastIndexOf(',') == OldFileList.Length - 1) OldFileList = OldFileList.Substring(0, OldFileList.Length - 1);
                return OldFileList;
            }
            return OldFileList;
        }

        protected static string GetModelName(string ConditionStr, string RegexStr, string StrName)
        {
            MatchCollection matchs = new Regex(RegexStr, RegexOptions.IgnoreCase).Matches(ConditionStr);
            if (matchs.Count > 0) return matchs[0].Groups[StrName].Value;
            MatchCollection matchs2 = new Regex(StrName + "=\"{(?<" + StrName + ">(\\w*.\\w*))}\"", RegexOptions.IgnoreCase).Matches(ConditionStr);
            if (matchs2.Count > 0) return ("{" + matchs2[0].Groups[StrName].Value + "}");
            return "";
        }

        public static DataTable GetSqlStrDataTabel(string SqlStr)
        {
            DataTable table = new DataTable();
            switch (SqlStr.Split(new char[] { '$' })[2].ToLower())
            {
                case "intro":
                {
                    DcmsIntroProcessCode code = new DcmsIntroProcessCode();
                    return code.IntroList(SqlStr);
                }
                case "cate":
                {
                    DcmsCateProcessCode code2 = new DcmsCateProcessCode();
                    return code2.CateList(SqlStr);
                }
                case "news":
                {
                    DcmsNewsProcessCode code3 = new DcmsNewsProcessCode();
                    return code3.NewsList(SqlStr);
                }
                case "products":
                {
                    DcmsProductProcessCode code4 = new DcmsProductProcessCode();
                    return code4.ProductList(SqlStr);
                }
                case "link":
                {
                    DcmsLinkProcessCode code5 = new DcmsLinkProcessCode();
                    return code5.LinkList(SqlStr);
                }
                case "down":
                {
                    DcmsDownProcessCode code6 = new DcmsDownProcessCode();
                    return code6.DownList(SqlStr);
                }
                case "vote":
                {
                    DcmsVoteProcessCode code7 = new DcmsVoteProcessCode();
                    return code7.VoteList(SqlStr);
                }
                case "guestbook":
                {
                    DcmsGuestBookProcessCode code8 = new DcmsGuestBookProcessCode();
                    return code8.GuestBookList(SqlStr);
                }
                case "position":
                {
                    DcmsPositionProcessCode code9 = new DcmsPositionProcessCode();
                    return code9.PositionList(SqlStr);
                }
                case "siteinfo":
                {
                    DcmsSiteInfoProcessCode code10 = new DcmsSiteInfoProcessCode();
                    return code10.SiteInfoList(SqlStr);
                }
            }
            return table;
        }
    }
}

